package com.common.frame.mvp.base;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.common.frame.utils.ToastUtil;

import io.reactivex.disposables.CompositeDisposable;


/**
 * 功能描述: Activity的基类，封装一些共有操作。要求项目中所有的Activity继承！
 */
public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IView {

    public CompositeDisposable mCompositeSubscription = new CompositeDisposable();
    protected ProgressDialog mProgressDialog;

    protected P mPresenter = null;
    /**
     * 返回注入的Presenter, 如果返回null则不可使用 {@code mPresenter}
     */
    @Nullable
    public abstract P createPresenter();

    /**
     * 返回布局layout
     */
    public abstract int getLayoutId();

    public abstract void initViews(Bundle savedInstanceState);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
        if (mPresenter != null) mPresenter.attachView(this, this);
        if (getLayoutId() != 0){
            setContentView(getLayoutId());
        }
        initViews(savedInstanceState);
        setLightSystem();
    }


    /**
     * 在调用activity的finish方法之前调用
     */
    protected void onBeforeFinish() {
        // 空实现，子类根据需要选择重写
    }

    protected void setStatusBar(int statusColor) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //设置状态栏颜色 透明
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (statusColor == 0){
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }else{
                getWindow().setStatusBarColor(statusColor);
            }
            setLightSystem();
        }
    }

    protected void setLightSystem(){
        //设置主题为亮色主题，状态栏文字为黑色。
        if (isLightSystem()){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }else{
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
    }

    /**
     * 是否设置亮色主题，亮色状态栏文字为黑色，否则为白色
     */
    protected boolean isLightSystem(){
        return true;
    }


    @Override
    protected void onDestroy() {
        onBeforeFinish();
        mCompositeSubscription.dispose();
        super.onDestroy();
    }

    /**
     * 显示进度对话框 (根据需要，子类可以选择性重写)
     *
     * @param msg 对话框显示的文字 如果为""或null,默认显示 加载中...
     */
    @Override
    public void showProgress(String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
        }
        mProgressDialog.setMessage(msg);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(true);
        if (mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.show();
    }

    /* 根据需要，子类可以选择性重写*/
    @Override
    public void hideProgress() {
        if (mProgressDialog!= null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showMsg(String message) {
        // 根据需要，子类可以选择性重写
        if (!TextUtils.isEmpty(message)){
            ToastUtil.showShortToast(message);
        }
    }


}